<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
</head>
<body>
<div class="ok-body">
    <!--面包屑导航区域-->
    <div class="ok-body-breadcrumb">
        <span class="layui-breadcrumb">
            <a><cite>首页</cite></a>
            <a><cite>会员管理</cite></a>
            <a><cite>用户列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </div>
    <!--模糊搜索区域-->
    <div class="layui-row">
        <form class="layui-form layui-col-md12 ok-search">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime">
            <input class="layui-input" placeholder="请输入用户名" autocomplete="off">
            <button class="layui-btn" lay-submit="" lay-filter="search">
                <i class="layui-icon">&#xe615;</i>
            </button>
        </form>
    </div>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script src="../../lib/nprogress/nprogress.js"></script>
<script>
    layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var okLayer = layui.okLayer;
        var okUtils = layui.okUtils;
        var okMock = layui.okMock;

        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});

        var userTable = table.render({
            elem: '#tableId',
            url: okMock.api.user.list,
            limit: 20,
            page: true,
            toolbar: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: "guid", title: "GUID", width: 280, sort: true},
                {field: "username", title: "账号", width: 100},
                {field: "password", title: "密码", width: 100},
                {field: "reallyName", title: "姓名", width: 100},
                {field: "nickname", title: "昵称", width: 100},
                {field: "role", title: "角色", width: 100, templet: "#roleTpl"},
                {field: "status", title: "状态", width: 100, templet: "#statusTpl"},
                {field: "email", title: "邮箱", width: 200},
				{field: "province", title: "省份", width: 120},
				{field: "city", title: "城市", width: 100},
				{field: "county", title: "区县", width: 120},
                {field: "createTime", title: "创建时间", width: 150},
                {field: "logins", title: "登陆次数", width: 100},
                {title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.info(res, curr, count);
            }
        });

        form.on("submit(search)", function (data) {
            userTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "batchEnabled":
                    batchEnabled();
                    break;
                case "batchDisabled":
                    batchDisabled();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "add":
                    add();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data);
                    break;
                case "del":
                    del(data.id);
                    break;
            }
        });

        function batchEnabled() {
            okLayer.confirm("确定要批量启用吗？", function (index) {
                // TODO
                layer.close(index);
                var idsStr = okUtils.table.batchCheck(table);
                if (idsStr) {
                    okUtils.ajax(okMock.api.user.batchNormal, "put", {idsStr: idsStr}).done(function (response) {
                        console.log(response);
                        okUtils.table.successMsg("批量启用成功");
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function batchDisabled() {
            okLayer.confirm("确定要批量停用吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.table.batchCheck(table);
                if (idsStr) {
                    okUtils.ajax(okMock.api.user.batchStop, "put", {idsStr: idsStr}).done(function (response) {
                        console.log(response);
                        okUtils.table.successMsg("批量停用成功");
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.table.batchCheck(table);
                if (idsStr) {
                    okUtils.ajax(okMock.api.user.batchDel, "delete", {idsStr: idsStr}).done(function (response) {
                        console.log(response);
                        okUtils.table.successMsg("批量删除成功");
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function add() {
            okLayer.open("添加用户", "user-add.html", "90%", "90%", null, function () {
                userTable.reload();
            })
        }

        function edit(data) {
            okLayer.open("编辑用户", "user-edit.html", "90%", "90%", function (layero) {
                var iframeWin = window[layero.find("iframe")[0]["name"]];
                iframeWin.initForm(data);
            }, function () {
                userTable.reload();
            })
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax(okMock.api.user.batchDel, "delete", {idsStr: id}).done(function (response) {
                    console.log(response);
                    okUtils.table.successMsg("删除成功");
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchEnabled">批量启用</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchDisabled">批量停用</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加用户</button>
    </div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>
<!-- 启用|停用模板 -->
<script type="text/html" id="statusTpl">
    {{#  if(d.status == 0){ }}
        <span class="layui-btn layui-btn-normal layui-btn-xs">已启用</span>
    {{#  } else if(d.status == 1) { }}
        <span class="layui-btn layui-btn-warm layui-btn-xs">已停用</span>
    {{#  } }}
</script>
<script type="text/html" id="roleTpl">
    {{#  if(d.role == 0){ }}
        <span class="layui-btn layui-btn-normal layui-btn-xs">超级会员</span>
    {{#  } else if(d.role == 1) { }}
        <span class="layui-btn layui-btn-warm layui-btn-xs">普通用户</span>
    {{#  } }}
</script>
</body>
</html>
